package com.sxt.permissionsdispatcher_processor.permissiondispatchercompiler

import java.util.*
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements

class MethodInfo(element: Elements, typeElement: TypeElement) {

    /**
     * 对应的类名
     */
    private var className: String

    /**
     * 对应的包名
     */
    var packageName: String

    /**
     * 生成代理分发类的文件名
     */
    val fileName: String

    companion object {
        /**
         * 生成类的代理名称
         */
        private const val PROXY_NAME = "PermissionProxy"
    }

    var grantMethodMap = HashMap<Int, String>()

    var deniedMethodMap = HashMap<Int, String>()

    var rationalMethodMap = HashMap<Int, String>()

    init {
        val packageElement = element.getPackageOf(typeElement)
        this.packageName = packageElement.qualifiedName.toString()
        this.className = ClassValidator.getClassName(typeElement, packageName)
        this.fileName = "${className}$$${PROXY_NAME}"
    }


    fun generateJavaCode(): String {
        return """
// 由编译自动生成的权限分发代码，请勿修改
package $packageName;

import com.sxt.permissionhelperlib.*;

public class $fileName implements $PROXY_NAME<$className> {
    
    ${generateGrantMethod()}

    ${generateDeniedMethod()}

    ${generateRationalMethod()}

}
        """.trimIndent()
    }

    private fun generateGrantMethod(): String {
        if (grantMethodMap.isEmpty()) {
            return ""
        }
        val grant = """
    @Override
    public void grant(int requestCode, $className source, String[] permissions) {
        switch (requestCode) { """

        val end = """
        }
    }
    """
        var content = ""
        if (grantMethodMap.isNotEmpty()) {
            grantMethodMap.forEach {
                content = """
            $content
            case ${it.key}:
                source.${it.value}(permissions);
                break;
                
            """
            }
        }
        return """
        $grant
        $content
        $end
        """
    }

    private fun generateDeniedMethod(): String {
        val grant = """
    @Override
    public void denied(int requestCode, $className source, String[] permissions) {
        switch (requestCode) { """

        val end = """
        }
    }
    """
        var content = ""
        if (deniedMethodMap.isNotEmpty()) {
            deniedMethodMap.forEach {
                content = """
            $content
            case ${it.key}:
                source.${it.value}(permissions);
                break;
                
            """
            }
        }
        return """
        $grant
        $content
        $end
        """
    }

    private fun generateRationalMethod(): String {
        val grant = """
    @Override
    public boolean rational(int requestCode, $className source, String[] permissions, PermissionRationalCallback rationalCallback) {
        switch (requestCode) { """

        val end = """
        }
        
        return false;
    }
    """
        var content = ""
        if (rationalMethodMap.isNotEmpty()) {
            rationalMethodMap.forEach {
                content = """
            $content
            case ${it.key}:
                source.${it.value}(permissions, rationalCallback);
                return true;
                
            """
            }
        }
        return """
        $grant
        $content
        $end
        """
    }
}